package hr.sting.droidorm.types;

import android.content.ContentValues;
import android.database.Cursor;
import hr.sting.droidorm.OrmException;

/**
 * @author mstipanov
 * @since 11.07.2010. 20:41:09
 */
public abstract class AbstractOrmTypeMapper implements OrmTypeMapper {
    private OrmTypeMapper next;

    public AbstractOrmTypeMapper(OrmTypeMapper next) {
        this.next = next;
    }

    @Override
    public Object getCursorValue(Cursor cursor, int index, Class<?> type) {
        if (isForMe(type)) {
            return actualGetCursorValue(cursor, index, type);
        }

        if (null != next) {
            return next.getCursorValue(cursor, index, type);
        }

        throw new OrmException("Can't get value of type: " + type + " from cursor.");
    }

    @Override
    public void putContentValue(ContentValues contentValues, String columnName, Class<?> type, Object o) {
        if (isForMe(type)) {
            actualPutContentValue(contentValues, columnName, type, o);
            return;
        }

        if (null != next) {
            next.putContentValue(contentValues, columnName, type, o);
            return;
        }

        throw new OrmException("Can't set value " + o + " of type: " + type + " from to ContentValues.");
    }

    protected abstract boolean isForMe(Class<?> type);

    protected abstract void actualPutContentValue(ContentValues contentValues, String columnName, Class<?> type, Object o);

    protected abstract Object actualGetCursorValue(Cursor cursor, int index, Class<?> type);
}
